//
//  XJAddressTableViewProxy.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/5/22.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJAddressTableViewProxy.h"
#import "XJAddressCell.h"
#import "XJAddressModel.h"

@implementation XJAddressTableViewProxy

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

/**这个方法不走检查
 是否给tableView设置了frame 。是否 添加到view上了。
 自从用了Masonry后染上了坏习惯,初始化tableView为 CGRectZero 了事。不给frame 代理方法不走  调试半天 也是哈哈哈哈了
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 
    XJAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XJAddressCellId"];
    
    XJAddressModel *model = self.dataArray[indexPath.row];
    
    [cell configureAddressCellWithName:model.name phone:model.phone address:model.address isDefault:model.isDefault];
    
    XJWeakSelf(self);
    // 事件回调
    cell.addressCellSelectButtonBlock = ^(BOOL isSelect) {
        if (weakself.addressTableProxySelectAddressBlock) {
            
            weakself.addressTableProxySelectAddressBlock(isSelect, indexPath);
        }
    };
    
    cell.addressCellEditButtonBlock = ^{
        if (weakself.addressTableProxyEditBlock) {
            
           XJAddressModel *model = self.dataArray[indexPath.row];
            
            weakself.addressTableProxyEditBlock(model);
        }
    };
    cell.addressCellDeleteButtonBlock = ^{
        if (weakself.addressTableProxyDeleteBlock) {
            
            weakself.addressTableProxyDeleteBlock(indexPath);
        }
    };
    
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    XJAddressModel *model =  self.dataArray[indexPath.row];
    
    if (self.addressTableProxyDidSelectedRowBlock) {
        self.addressTableProxyDidSelectedRowBlock(model);
    }
    
//    XJLog(@"选择的地址是:%@",model);
}


@end
